package com.zmn.plat.business.impl.sendtemplate;

import java.util.Objects;

import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.utils.MQPushUtil;
import com.zmn.plat.business.interfaces.sendtemplate.SendSmsInfoBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.manager.constant.ZmnSmsConsts;
import com.zmn.plat.model.entity.message.MsgTemplate;
import com.zmn.plat.model.entity.push.ZmnSms;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述：通过创蓝短信发送消息
 *
 * @author wangjie
 * @since 2020/6/19 10:46
 **/
@Slf4j
public class CLSendSmsInfoBServiceImpl implements SendSmsInfoBService {

    private static final String TAG = "通过创蓝短信发送消息";
    private volatile static CLSendSmsInfoBServiceImpl clSendSmsInfoBService;

    private CLSendSmsInfoBServiceImpl() {}

    @Override
    public Integer doSend(MsgTemplate msgTemplate, ZmnSms zmnSms) {

        // 验证必要条件
        if (StringUtil.isBlank(msgTemplate.getMessage())) {
            log.error("[{}],发送失败，短信内容不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_CL_NO_CONTENT, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_CL_NO_CONTENT);
        }
        if (StringUtil.isBlank(msgTemplate.getMessageAccount())) {
            log.error("[{}],发送失败，通道账号不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_CL_NO_ACCOUNT, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_CL_NO_ACCOUNT);
        }
        if (StringUtil.isBlank(msgTemplate.getMessagePwd())) {
            log.error("[{}],发送失败，通道密码不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_CL_NO_PASSWORD, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_CL_NO_PASSWORD);
        }

        // 拼装签名
        String msg = StringUtil.isBlank(msgTemplate.getMessageMark()) ? msgTemplate.getMessage() : "【" + msgTemplate.getMessageMark() + "】" + msgTemplate.getMessage();
        ResultDTO result = MQPushUtil.sendChuangLanTemplateInfoByTime(zmnSms.getParams(), msg, DateUtil.getNowFormatted(DateUtil.FORMAT_DATETIME3), msgTemplate.getMessageAccount(),
            msgTemplate.getMessagePwd(), msgTemplate.getMessageType());
        log.info("[{}],发送参数。 params:{},msg:{},time:{}", TAG, zmnSms.getParams(), msg, DateUtil.getNowFormatted(DateUtil.FORMAT_DATETIME3));

        if (result.getStatusBool()) {
            log.info("[{}],发送成功。 code:{},msg:{}", TAG, ZmnSmsConsts.MSG_SEND_SUCCESS, ZmnSmsConsts.MSG_SEND_SUCCESS_STR);
            return ZmnSmsConsts.MSG_SEND_SUCCESS;
        } else {
            log.info("[{}],发送失败。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_CL_ERROR, result.getMessage());
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_CL_ERROR);
        }
    }

    /**
     * 描述：获取该类的单例对象
     *
     * @return com.zmn.plat.business.impl.sendtemplate.ZmnCLSendSmsInfoBServiceImpl
     * @author wangjie
     * @since 2020/6/19 10:47
     **/
    public static CLSendSmsInfoBServiceImpl getSingleton() {
        if (Objects.isNull(clSendSmsInfoBService)) {
            synchronized (CLSendSmsInfoBServiceImpl.class) {
                if (Objects.isNull(clSendSmsInfoBService)) {
                    clSendSmsInfoBService = new CLSendSmsInfoBServiceImpl();
                }
            }
        }
        return clSendSmsInfoBService;
    }
}
